package Com.Ring.TradingMobile.Lib;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.R;
import android.app.Activity;
import android.util.Log;
import android.widget.TextView;

public class Common {

	public static final String KeyIntentThiTruong = "ThiTruong";
	public static final String DanhMucRoot = "Main";
	public static String TAG = "Common";
	private static   Thread myThread = null;
	private static boolean IsRunningTimer;
	public static Activity activity;
	
	
	public static enum MARKET {
		HOSE, HNX, UPCOM
	}


	public static int ConvertToIntMarket(MARKET market) {
		switch (market) {
			case HOSE :
				return 1;
			case HNX :
				return 2;
			case UPCOM :
				return 3;
			default :
				return 1;
		}
	}
	
	public static MARKET ConvertToEnumMarket(int marketId) {
		switch(marketId) {
			case 1:
				return MARKET.HOSE;
			case 2:
				return MARKET.HNX;
			case 3:
				return MARKET.UPCOM;
			default:
				return MARKET.HOSE;
		}
	}

	public static String FormatNumber(float number) {
		NumberFormat formatter = new DecimalFormat("#0.0#");
		return formatter.format(number);
	}
	public static String FormatShortNumber(float number) {
		float Bilion = 1000000000;
		float Milion = 1000000;
		float Thousand = 1000;
		if (Math.abs(number) >= Bilion) {
			float dbTemp = (float) number / Bilion;
			return FormatNumber(dbTemp) + " T";
		} else if (Math.abs(number) >= Milion) {
			float dbTemp = (float) number / Milion;
			return FormatNumber(dbTemp) + " Tr";
		} else if (Math.abs(number) >= Thousand) {
			float dbTemp = (float) number / Thousand;
			return FormatNumber(dbTemp) + " N";
		} else {
			return FormatNumber(number);
		}
	}

	public static int GetMarketStatus(MARKET marketEnum, String status)
    {
        switch (marketEnum)
        {
            case HOSE:
                    if(status.toUpperCase().equals("P"))
                       return Com.Ring.TradingMobile.R.string.strSession1;
                    else if(status.toUpperCase().equals("O"))
                    	return Com.Ring.TradingMobile.R.string.strSession2;
                    else if(status.toUpperCase().equals("A"))
                    	return Com.Ring.TradingMobile.R.string.strSession3;
                    else if(status.toUpperCase().equals("C"))
                    	return Com.Ring.TradingMobile.R.string.strAgreement;
                    else if(status.toUpperCase().equals("K"))
                    	return Com.Ring.TradingMobile.R.string.strClose;
            case HNX:
            	if(status.toUpperCase().equals("O"))
                    return Com.Ring.TradingMobile.R.string.strOpen;
                 else if(status.toUpperCase().equals("C"))
                 	return Com.Ring.TradingMobile.R.string.strClose;
            case UPCOM:
            	if(status.toUpperCase().equals("O") || status.toUpperCase().equals("U"))
                    return Com.Ring.TradingMobile.R.string.strOpen;
                 else if(status.toUpperCase().equals("H") || status.toUpperCase().equals("C"))
                 	return Com.Ring.TradingMobile.R.string.strClose;
            	
            default: return Com.Ring.TradingMobile.R.string.strUnvailiable;
        }
    }
	
	public static void StartTimer(Runnable runable, Activity act) {
		IsRunningTimer =true;
		activity = act;
		if(myThread!=null && myThread.isAlive()) {
			myThread.stop();
		}
	    myThread = new Thread(runable);  
	    myThread.start();
	}
	public static void StopTimer() {
		IsRunningTimer = false;
	}
	
	public static void doWork() {
		
		activity.runOnUiThread(new Runnable() {
	        public void run() {
	            try{
	                TextView txtCurrentTime= (TextView)activity.findViewById(Com.Ring.TradingMobile.R.id.txtTimer);
	                DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
	                Date date = new Date();
	                txtCurrentTime.setText(dateFormat.format(date));
	            }catch (Exception e) {}
	        }
	    });
	}
	
	public static class CountDownRunner implements Runnable{
	    // @Override
	    public void run() {
	            while(IsRunningTimer){
	                try {
	                doWork();
	                    Thread.sleep(1000);
	                    
	                } catch (InterruptedException e) {
	                        Thread.currentThread().interrupt();
	                }catch(Exception e){
	                }
	            }
	    }
	    
	}
}
